package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.common.R;
import com.itheima.reggie.common.ThisYearMonths;
import com.itheima.reggie.entity.*;
import com.itheima.reggie.mapper.OrderDetailDao;
import com.itheima.reggie.service.*;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/analyse")
@Api(tags = "数据分析相关接口")
public class AnalysisController {

    @Autowired
    private OrderDetailService orderDetailService;

    @Autowired
    private OrderService orderService;

//    查询菜系营收占比
    @GetMapping("/dishCash")
    public R<List<Analyse>> selectDishCash(){
/*//        条件构造器
        LambdaQueryWrapper<Category> categoryLambdaQueryWrapper = new LambdaQueryWrapper<>();
//        构建为菜系的条件
        categoryLambdaQueryWrapper.eq(Category::getType,1);
//        查询是菜系的数据
        List<Category> categoryList = categoryService.list(categoryLambdaQueryWrapper);
//        查询每个菜系下面有多少菜品之前，要对该菜系有没有菜品进行非空判断

        for (Category category : categoryList) {
            LambdaQueryWrapper<Dish> dishLambdaQueryWrapper = new LambdaQueryWrapper<>();
            dishLambdaQueryWrapper.eq(category.getId() != null,Dish::getCategoryId,category.getId());
            List<Dish> dishList = dishService.list(dishLambdaQueryWrapper);
            if ( null != dishList){
                for (Dish dish : dishList) {
                    LambdaQueryWrapper<OrderDetail> orderDetailLambdaQueryWrapper = new LambdaQueryWrapper<>();
                    orderDetailLambdaQueryWrapper.
                }
            }

        }*/
        List<Analyse> analyseList = orderDetailService.dishCount();

        return R.success(analyseList);
    }
//    套餐营收占比分析
    @GetMapping("/setmealCash")
    public  R<List<Analyse>> selectSetmealCash(){
       List<Analyse> analyseList = orderDetailService.setmealCount();
       return  R.success(analyseList);
    }
//    菜系销量占比分析
@GetMapping("/dishNum")
public  R<List<Analyse>> selectDishNum() {
    List<Analyse> analyseList = orderDetailService.dishNum();
    return R.success(analyseList);
}
//    套餐销量占比分析
    @GetMapping("/setmealNum")
    public  R<List<Analyse>> selectSetmealNum() {
        List<Analyse> analyseList = orderDetailService.setmealNum();
        return R.success(analyseList);
    }
//    月营业额趋势图
    @GetMapping("/cashPerMonth")
    public Map<String,Object> selectSale(){
        List<String> monthList = ThisYearMonths.getThisYearMonths();
        List<Integer> dataList = new ArrayList<>();
        HashMap<String, Object> map = new HashMap<>();
        for (String month : monthList) {
            Integer cash = orderService.cash(month);
            if (null != cash){
                dataList.add(cash);
            }else {
                dataList.add(0);
            }
        }

        map.put("dataList",dataList);
        map.put("monthList",monthList);
        log.info("dataList集合数据：{}",dataList);

        return map;
    }
}
